#include"stack.h"


void StackInit(Stack* ps)
{
	ps->_a = new STDataType[8];
	ps->_capacity = 8;
	ps->_top = -1;
}

void StackPush(Stack* ps, STDataType data)
{
	if (ps->_capacity == ps->_top + 1)
	{
		STDataType* newdata = (STDataType*)realloc(ps->_a, sizeof(STDataType) * ps->_capacity * 2);
		if (newdata != NULL)
		{
			ps->_a = newdata;
			ps->_capacity *= 2;
		}
		else
		{
			exit(-1);
		}
	}
	
	ps->_a[++ps->_top] = data;
}

void StackPop(Stack* ps)
{
	if (ps->_top >= 0)
		ps->_top--;
	else
		perror("POP:");
}

STDataType StackTop(Stack* ps)
{
	if (ps->_top == -1)
	{
		perror("TOP:");
		exit(-1);
	}
		
		
	return ps->_a[ps->_top];
}

int StackSize(Stack* ps)
{
	return ps->_top + 1;
}

int StackEmpty(Stack* ps)
{
	if (ps->_top == -1)
	{
		return 1;
	}

	return 0;
}

void StackDestroy(Stack* ps)
{
	if (ps != nullptr && ps->_a != nullptr)
	{
		delete[] ps->_a;
		ps->_a = nullptr;
		ps->_capacity = 0;
		ps->_top = -1;
	}
}
